import java.lang.invoke.LambdaConversionException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode {
       public char val;
       public TreeNode left;
       public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;

        B.left = D;
        B.right = E;

        C.left = F;
        C.right = G;

        D.left = H;

        return A;
    }

    //1.先序遍历/前序遍历
    public void preOrder(TreeNode root ) {
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //2.中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    //3.后序遍历
    public void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    public int NodeSize;
    // 获取树中节点的个数
    public int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        NodeSize++;
        size(root.left);
        size(root.right);
        return NodeSize;
    }
    public int size2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size2(root.left) + size2(root.right)
                +1;
    }

    public int LeafCount;
    // 获取叶子节点的个数
    public int getLeafNodeCount(TreeNode root){
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            LeafCount++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return LeafCount;
    }

    public int getLeafNodeCount2(TreeNode root){
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) +
                getLeafNodeCount2(root.right);
    }

    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) +
                getKLevelNodeCount(root.right,k-1);

    }

    // 获取二叉树的高度
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int l = getHeight(root.left) ;
        int r = getHeight(root.right) ;
        return Math.max(l,r) + 1;
    }

    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, int val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode leftVal = find(root.left,val);
        if(leftVal != null) {
            return leftVal;
        }
        TreeNode rightVal = find(root.right,val);
        if(rightVal != null) {
            return rightVal;
        }
        return null;
    }

    /*
    翻转二叉树
    class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        if(root != null) {
            TreeNode tmp = root.left;
            root.left = root.right;
            root.right = tmp;
        }
        invertTree(root.left);
        invertTree(root.right);
        return root;

    }
}
     */


    /*
    相同的树
    class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p != null && q == null || p == null && q != null) {
            return false;
        }
        if(p == null && q == null) {
            return true;
        }
        if(p.val != q.val  ) {
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);

    }
}
     */

    /*
    另一个树的子树
    class Solution {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null) {
            return false;
        }
        if(isSameTree(root,subRoot)) {
            return true;
        }
        if(isSubtree(root.left,subRoot)) {
            return true;
        }
        if(isSubtree(root.right,subRoot)) {
            return true;
        }
        return false;

    }
        public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p != null && q == null || p == null && q != null) {
            return false;
        }
        if(p == null && q == null) {
            return true;
        }
        if(p.val != q.val  ) {
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);

    }

}
     */

    /*
    平衡二叉树1
    class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        if(Math.abs(leftHeight-rightHeight) < 2 && isBalanced(root.left) && isBalanced(root.right)) {
            return true;
        }
        return false;
    }
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int l = getHeight(root.left) ;
        int r = getHeight(root.right) ;
        return Math.max(l,r) + 1;
    }
}
     */

    /*
    平衡二叉树2

    class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        }
       return getHeight(root) >= 0;
    }
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int l = getHeight(root.left) ;
        if( l < 0) {
            return -1;
        }
        int r = getHeight(root.right) ;
        if( l >= 0 && r >= 0 && Math.abs(l-r) < 2  ) {
            return Math.max(l,r) + 1;
        } else {
            return -1;
        }

    }
}
     */


    /*
    对称二叉树
    class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null) {
            return true;
        }
        return isSymmetricChild(root.left,root.right);

    }
    public boolean isSymmetricChild(TreeNode left, TreeNode right) {
        if(left == null && right != null || left != null && right == null) {
            return false;
        }
        if(left == null && right == null) {
            return true;
        }
        if(left.val == right.val && isSymmetricChild(left.left,right.right) &&
        isSymmetricChild(left.right,right.left)) {
            return true;
        }
        return false;
    }
}
     */



    /*
     二叉树遍历
    import java.util.Scanner;
class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val) {
        this.val = val;
    }
}
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
           String str = in.nextLine();
           Main main = new Main();
           TreeNode root = main.createTree(str);
           //中序遍历
            inOrder(root);
        }
    }

        public  int i;
    public  TreeNode createTree(String str) {

        TreeNode root = null;
        char ch = str.charAt(i);
        if(ch != '#') {
            root = new TreeNode(ch);
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        } else {
            i++;
        }

        return root;

    }
    public static void inOrder(TreeNode root) {
        if(root == null) {
            return ;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);

    }
}
     */

    //层序遍历
    public void levelOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

    /*class Solution {
        public List<List<Integer>> levelOrder2(TreeNode root) {
            List<List<Integer>> ret = new ArrayList<>();
            if(root == null) {
                return ret;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while(!queue.isEmpty()) {
                int size = queue.size();
                List<Integer> list = new ArrayList<>();
                while(size != 0) {
                    TreeNode cur = queue.poll();
                    list.add(cur.val);
                    size--;
                    if(cur.left != null) {
                        queue.offer(cur.left);
                    }
                    if(cur.right != null) {
                        queue.offer(cur.right);
                    }
                }
                ret.add(list);

            }
            return ret;
        }
    }*/

    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if(root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            } else {
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                return false;
            }
        }
        return  true;

    }




















}
